#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;

void Swap(int &a, int &b)
{
	a^=b;
	b^=a;
	a^=b;
}

void rotate(int *matrix[],int n)
{
	for(int i=0;i<n;i++)	
		for(int j=i+1;j<n;j++)
		{
			Swap( *((int*)matrix+n*i+j),*((int*)matrix+n*j+i) );//Swap(matrix[i][j],matrix[j][i])
			
		}

	for(int i=0;i<n/2;i++)
		for(int j=0;j<n;j++)
		{
			Swap( *((int*)matrix+n*i+j),*((int*)matrix+n*(n-1-i)+j) );  //Swap(matrix[i][j],matrix[n-1-i][j])
		}
}

void display(int **matrix,int n)
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			cout<<*((int*)matrix+n*i+j)<<" ";//matrix[i][j]
		cout<<endl;
	}
	cout<<endl;
}

int main()
{
	int dimension;
	
	int a[4][4]=
	{
		{1,2,3,4},
		{5,6,7,8},
		{9,10,11,12},
		{13,14,15,16}
	};
	dimension=(int)sqrt(sizeof(a)/4);
	cout<<"dimension:"<<dimension<<endl;
	cout<<"before rotate:"<<endl;
	display((int**)a,dimension);
	rotate((int**)a,dimension);
	cout<<"after rotate:"<<endl;
	display((int**)a,dimension);

	int b[3][3]=
	{
		{1,2,3},
		{4,5,6},
		{7,8,9}
	};
	dimension=(int)sqrt(sizeof(b)/4);
	cout<<"dimension:"<<dimension<<endl;
	cout<<"before rotate:"<<endl;
	display((int**)b,dimension);
	rotate((int**)b,dimension);
	cout<<"after rotate:"<<endl;
	display((int**)b,dimension);
	return 0;
}

